package com.zrrd.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zrrd.reggie.bean.*;

import com.zrrd.reggie.common.ThreadLocalUtil;
import com.zrrd.reggie.controller.AddressBookController;
import com.zrrd.reggie.mapper.AddressBookMapper;
import com.zrrd.reggie.mapper.OrdersMapper;
import com.zrrd.reggie.mapper.UserMapper;
import com.zrrd.reggie.service.OrderDetailService;
import com.zrrd.reggie.service.OrdersService;
import com.zrrd.reggie.service.ShoppingCartService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.xml.ws.Action;
import java.awt.*;
import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;

@Service
public class OrdersServiceImpl extends ServiceImpl<OrdersMapper, Orders> implements OrdersService {

    @Autowired
    private ShoppingCartService shoppingCartService;

    @Autowired
    private OrderDetailService orderDetailService;

    @Autowired
    private OrdersMapper ordersMapper;

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private AddressBookMapper addressBookMapper;

    //用户下单
    @Override
    @Transactional
    public void submit(Orders orders) {
        //获取用户id
        Long userId = ThreadLocalUtil.getCurrentId();
        //查询购物车列表
        QueryWrapper<ShoppingCart> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id",userId);
        List<ShoppingCart> shoppingCartList = shoppingCartService.list(wrapper);

        //获取用户信息
        User user = userMapper.selectById(userId);

        //获取地址信息
        AddressBook addressBook = addressBookMapper.selectById(orders.getAddressBookId());

        //生成订单编号
        Long orderNumber = IdWorker.getId();

        //创建订单明细集合
        List<OrderDetail> orderDetailList = new ArrayList<>();

        //创建计算总金额的对象
        AtomicInteger amount = new AtomicInteger(0);

        //遍历购物车集合
        for (ShoppingCart cart : shoppingCartList) {
            //将购物车中的属性设置为订单明细的对象
            OrderDetail orderDetail = new OrderDetail();
            orderDetail.setOrderId(orderNumber);
            orderDetail.setNumber(cart.getNumber());
            orderDetail.setDishId(cart.getDishId()); //菜品id
            orderDetail.setDishFlavor(cart.getDishFlavor()); //口味
            orderDetail.setSetmealId(cart.getSetmealId()); //套餐id
            orderDetail.setName(cart.getName());
            orderDetail.setImage(cart.getImage());
            orderDetail.setAmount(cart.getAmount());
            //进行金额的累乘计算
            amount.addAndGet(cart.getAmount().multiply(new BigDecimal(cart.getNumber())).intValue());
            //将明细对象添加进明细集合
            orderDetailList.add(orderDetail);
        }

        //组装订单数据
        orders.setId(orderNumber);
        orders.setNumber(orderNumber+"");
        orders.setStatus(2);
        orders.setUserId(ThreadLocalUtil.getCurrentId());
        orders.setOrderTime(LocalDateTime.now());
        orders.setCheckoutTime(LocalDateTime.now());
        orders.setAmount(new BigDecimal(amount.get()));
        orders.setPhone(addressBook.getPhone());
        orders.setAddress(addressBook.getDetail());
        orders.setUserName(user.getName());
        orders.setConsignee(addressBook.getConsignee());

        //添加订单
        ordersMapper.insert(orders);

        //添加订单明细
        orderDetailService.saveBatch(orderDetailList);

        //清空购物车
        shoppingCartService.remove(wrapper);

    }

    @Override
    public void again(Orders orders){
        //根据订单id查询订单明细
        QueryWrapper<OrderDetail> wrapper = new QueryWrapper<>();
        wrapper.eq("order_id",orders.getId());
        List<OrderDetail> detailList = orderDetailService.list(wrapper);
        //新建购物车集合
        List<ShoppingCart> cartList = new ArrayList<>();
        for (OrderDetail detail : detailList) {
            ShoppingCart cart = new ShoppingCart();
            BeanUtils.copyProperties(detail,cart);
            //给用户车对象设置用户id
            cart.setUserId(ThreadLocalUtil.getCurrentId());
            //创建时间
            cart.setCreateTime(LocalDateTime.now());
            //将购物车对象添加进购物车集合
            cartList.add(cart);
        }
        //保存数据到购物车（批量
        shoppingCartService.saveBatch(cartList);
    }
}
